import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    list:[],
    commentList:[],
    comments:{}
  },
  mutations: {
    setState(state,payload){
      state[payload.key]=payload.value
    }
  },
  actions: {
    async http({commit}){
      let res = await axios.get('/main')
      console.log(res)
      commit('setState', {key:'list',value:res.data.data})
    },
    async watchComment({commit},payload){
      let res = await axios.get('/main/comments',{
        params:{authorId:payload}
      })
      await commit('setState', {key:'commentList',value:res.data.data})
      await commit('setState', {key:'comments',value:res.data.arr})
    },
    async addComment({commit}, payload){
      let res= await axios.put('/main/comments',  {...payload})
      if(res.data.code===200){
        await commit('setState', {key:'commentList',value:res.data.data})
      }
    }
  },
  modules: {
  }
})
